package vn.student.controller;

import java.util.ArrayList;
import java.util.Date;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.mylib.Assignment;
import vn.student.mylib.Constants;
import vn.student.mylib.Contact;
import vn.student.mylib.ListContactAdapter;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class ViewAssignmentDetailController {
	Activity act;
	AStudentDB myDB = null;
	String id;
	public Assignment assignmentInfo;
	boolean isCreateNew = false;
	public ViewAssignmentDetailController(Activity act,String id, boolean isCreateNew, String classId){
		this.act = act;
		this.id = id;
		myDB = new AStudentDB(act);
		this.isCreateNew = isCreateNew;
		if(isCreateNew){
			Date tmp = new Date();
			String dateStr = "";
			String str = "";
			str = String.valueOf(tmp.getYear()+1900);
			dateStr += str + "-";
			str = String.valueOf(tmp.getMonth()+1);
			str = str.length()==2?str:("0"+str);
			dateStr += str + "-";
			str = String.valueOf(tmp.getDate());
			str = str.length()==2?str:("0"+str);
			dateStr += str;
			assignmentInfo = new Assignment("-1","New Assignment",2,"New assignment",dateStr,"00:00:00",false,classId);
		}
		else
			assignmentInfo = myDB.getAssignmentDetail(id);
	}
	public void showAssDetail(){
		if(assignmentInfo!=null){
			TextView label = (TextView)act.findViewById(R.id.assNameLabel);
			label.setText(assignmentInfo.name); 
			label = (TextView)act.findViewById(R.id.dueDateLabel);
			label.setText(assignmentInfo.dueDate);
			label = (TextView)act.findViewById(R.id.priorityLabel);
			label.setText(Constants.priority[assignmentInfo.priority]);
			label = (TextView)act.findViewById(R.id.alertTimeLabel);
			label.setText(assignmentInfo.alertTime);
			label = (TextView)act.findViewById(R.id.contentLabel);
			label.setText(assignmentInfo.des);
			ImageButton tmp = (ImageButton) act.findViewById(R.id.isDoneBtn);
			if(assignmentInfo.done)
				tmp.setBackgroundResource(R.drawable.ass_done);
			else
				tmp.setBackgroundResource(R.drawable.ass_undone);
		}else{
			
		}
		showListPartner();
	}
	private void showListPartner(){
		ArrayList<Contact> listPartner = myDB.getPartnerList(id); 
		if(listPartner!=null){
			ListView listPartnerView = (ListView) act.findViewById(R.id.contactListView);
			listPartnerView.setAdapter(new ListContactAdapter(act,listPartner));
			listPartnerView.setOnCreateContextMenuListener(new OnCreateContextMenuListener()  {
				@Override
				public void onCreateContextMenu(ContextMenu menu, View v,
						ContextMenuInfo menuInfo) {
					menu.add(0,Constants.ASS_CONTEXTMENU_ADDPARTNER,0, "Add Partner");
					menu.add(0,Constants.ASS_CONTEXTMENU_DELETEITEM,1, "Delete");
				}
           });
		}
		
	}
	public void editDueDate(String dateStr){
		assignmentInfo.dueDate = dateStr;
		((TextView)act.findViewById(R.id.dueDateLabel)).setText(assignmentInfo.dueDate);
	}
	public void editAlertTime(String dateStr, String timeStr) {
		// TODO Auto-generated method stub
	}
	public boolean removePartner(String contactId) {
		return myDB.removePartner(assignmentInfo.id, contactId);
	}
	public ArrayList<Contact> getPartner() {
		return myDB.getStudentList(assignmentInfo.id);
	}
	public void addPartner(String contactId) {
		myDB.addPartner(assignmentInfo.id,contactId);
	}
	public void updateAssignment() {
		myDB.updateAssignmentDetail(assignmentInfo);		
	}
	public void editPriority(int arg2) {
		assignmentInfo.priority = arg2;
		((TextView)act.findViewById(R.id.priorityLabel)).setText(Constants.priority[assignmentInfo.priority]);
	}
	public void editAssName(String name) {
		assignmentInfo.name = name;
		((TextView)act.findViewById(R.id.assNameLabel)).setText(name);
	}
	public void addNewAssignment() {
		myDB.addNewAssignment(assignmentInfo);
	}
	public void deleteAssignment() {
		myDB.removeAssignment(assignmentInfo.id);
	}
	public void editDone() {
		assignmentInfo.done = !assignmentInfo.done;
		this.updateAssignment();
		ImageButton imgBtn = (ImageButton) act.findViewById(R.id.isDoneBtn);
		if(assignmentInfo.done)
			imgBtn.setBackgroundResource(R.drawable.ass_done);
		else
			imgBtn.setBackgroundResource(R.drawable.ass_undone);
	}
	public void editDesName(String des) {
		assignmentInfo.des = des;
		((TextView)act.findViewById(R.id.contentLabel)).setText(des);
	}
}